//
//  MotionSensor.m
//  UAVClient
//
//  Created by visu4l on 11. 7. 1..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MotionSensor.h"


@implementation MotionSensor

@synthesize motion;
@synthesize attitude;

@synthesize roll;
@synthesize pitch;
@synthesize yaw;

-(id)init{
    
    motionManager = [[CMMotionManager alloc] init];
    motionManager.deviceMotionUpdateInterval = 1.0f/20.0f;
    
    if(motionManager.gyroAvailable){
        [motionManager startDeviceMotionUpdates];
        
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0/20.0 target:self 
                                               selector:@selector(updateGyro) userInfo:nil repeats:YES];
    }else{
        [motionManager release];
    }

    return self;
}

-(void) updateGyro{
    
    motion = motionManager.deviceMotion;
    attitude = motion.attitude;
    
    roll = (short) (attitude.roll*100);   // range :  -314 ~ 314
    pitch = (short) (attitude.pitch*100); // range : -150 ~ 150
    yaw = (short) (attitude.yaw*100);     // range : -314 ~ 314
    
}

- (void)dealloc{
    [motionManager release];
    [super dealloc];
}

@end
